package cn.wolfcode.web.controller;

import cn.wolfcode.qo.CustomerReportQueryObject;
import cn.wolfcode.service.ICustomerReportService;
import cn.wolfcode.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") CustomerReportQueryObject qo){
        PageInfo pageInfo = customerReportService.selectForList(qo);
        model.addAttribute("pageResult", pageInfo);


        return "customerReport/list";
    }

    /**
     * 柱状图(不分页)
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/listBar")
    public String listBar(Model model,@ModelAttribute("qo") CustomerReportQueryObject qo){
        //查询数据库的分组数据
        List<HashMap> list = customerReportService.selectAll(qo);
        List x = new ArrayList();
        List y = new ArrayList();
        //遍历通过sql查出来的集合list
        for (HashMap map : list){
            x.add(map.get("groupType"));
            y.add(map.get("number"));
        }
        model.addAttribute("x", JSON.toJSONString(x));
        model.addAttribute("y",JSON.toJSONString(y));

        //分组类型转为文字显示
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));

        return "customerreport/listBar";
    }

    /**
     * 饼图(不分页)
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("/listPie")
    public String listPie(Model model,@ModelAttribute("qo") CustomerReportQueryObject qo){
        //查询数据库的分组数据
        List<HashMap> list = customerReportService.selectAll(qo);
        List legend = new ArrayList();
        List series = new ArrayList();
        //遍历通过sql查出来的集合list
        for (HashMap map : list){
            legend.add(map.get("groupType"));
            HashMap temp = new HashMap<>();
            temp.put("name", map.get("groupType"));
            temp.put("value", map.get("number"));
            series.add(temp);

        }
        model.addAttribute("legend", JSON.toJSONString(legend));
        model.addAttribute("series",JSON.toJSONString(series));

        //分组类型转为文字显示
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "customerreport/listPie";
    }

}
